package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.mess.ArticleVisitStreamMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * ClassName: HotArticleIncrHandleListener
 * Description:
 * date: 2023/8/27 0027 19:44
 *
 * @author zHan
 */
@Component
@Slf4j
public class HotArticleIncrHandleListener {

    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void onMessage(String msg) {
        if (StringUtils.isNotBlank(msg)) {
            ArticleVisitStreamMess mess = JSON.parseObject(msg,
                    ArticleVisitStreamMess.class);
            // 更新文章分值，同时更新redis缓存
            apArticleService.updateScore(mess);
        }
    }
}
